# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :channel.py
# @Time      :2022/7/30 16:03
# @Author    :吕华龙
from flask import Blueprint, jsonify
from flask_restful import Resource, Api, reqparse, marshal
from models.model import *
from models.fields import *

channel_bp = Blueprint("channel_bp", __name__, url_prefix="/channel")

api = Api(channel_bp)


# 轮播图视图
class ChannelView(Resource):
    def get(self):
        # 获取全部轮播图
        channel = ChannelModel.query.all()
        # 构造数据结构
        list_1 = []
        for i in channel:
            list_1.append({
                "id": i.id,
                "banner_img": i.banner_img,
                "link_type": i.link_type,
                "link_content": i.link_content
            })
        # 序列化
        channel_marshal = marshal(list_1, channel_fields)
        print("------------", channel_marshal)
        # 返回响应
        return jsonify({'code': 200, 'msg': '获取轮播图成功', 'data': channel_marshal})


# 创建路由
# 获取轮播图数据
api.add_resource(ChannelView, "/channel")
